import 'package:color_dart/RgbaColor.dart';
import 'package:flutter/material.dart';
import 'package:im_flutter_sdk_example/components/a_button/index.dart';
import 'package:im_flutter_sdk_example/components/activity/activity_list_item.dart';
import 'package:im_flutter_sdk_example/components/labels/index.dart';
import 'package:im_flutter_sdk_example/jsonserialize/active/data.dart';
import 'package:im_flutter_sdk_example/jsonserialize/need/data.dart';
import 'package:im_flutter_sdk_example/jsonserialize/serve/data.dart';
import 'package:im_flutter_sdk_example/pages/serve/widgets/serve_item.dart';
import 'package:im_flutter_sdk_example/pages/toolbar/alliance/widgets/tabTypeBtn.dart';
import 'package:im_flutter_sdk_example/pages/toolbar/home/widgets/need_list.dart';
import 'package:im_flutter_sdk_example/pages/toolbar/alliance/widgets/groupitem.dart';
import 'package:im_flutter_sdk_example/utils/constants.dart';
import 'package:im_flutter_sdk_example/utils/global.dart';
import 'package:im_flutter_sdk_example/mock/data.dart';
import 'package:im_flutter_sdk_example/jsonserialize/home/data.dart';

class Achievement extends StatelessWidget {
  final bool showFollow;
  const Achievement({
    Key key,
    this.showFollow = false,
  }) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () => {G.pushNamed('/achievement_detail')},
      child: Container(
        // padding: EdgeInsets.symmetric(vertical: 10),
        color: Colors.white,
        // height: 204,
        // margin: EdgeInsets.only(bottom: 10),
        child: Column(children: [
          if (showFollow)
            Container(
              padding: EdgeInsets.only(top: 6, bottom: 10),
              child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Row(
                      children: [
                        SizedBox(
                          width: 28,
                          height: 28,
                          child: Image.asset(
                            'lib/assets/images/default_avatar.png',
                            fit: BoxFit.cover,
                          ),
                        ),
                        Container(
                            child: Text('云南国科智融科技发展有限公司',
                                style: TextStyle(
                                    color: rgba(136, 135, 135, 1),
                                    fontSize: 14)),
                            margin: EdgeInsets.only(left: 10)),
                      ],
                    ),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Container(
                            height: 22,
                            padding: EdgeInsets.symmetric(horizontal: 6),
                            decoration: BoxDecoration(
                              borderRadius: BorderRadius.circular(11),
                              border: Border.all(
                                color: rgba(25, 109, 236, 1),
                                width: 1,
                              ),
                            ),
                            child: Text('+ 关注',
                                style: TextStyle(
                                    color: rgba(25, 109, 236, 1),
                                    fontSize: 12)),
                            margin: EdgeInsets.only(left: 3)),
                      ],
                    ),
                  ]),
            ),
          Container(
            height: 77,
            child: Row(
              children: [
                SizedBox(
                  width: 107,
                  height: 77,
                  child: Image.asset(
                    'lib/assets/images/order/order1.png',
                    fit: BoxFit.cover,
                  ),
                ),
                Padding(
                  padding: const EdgeInsets.only(left: 10),
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text('精密视觉检测项目（中科慧远）', style: mainTitleStyle),
                      Row(
                        children: [
                          LabelTuijian(),
                          spaceL(
                            AButton.normal(
                                width: 44,
                                height: 17,
                                bgColor: rgba(232, 243, 255, 1),
                                type: 'primary',
                                onPressed: () {},
                                child: Text('医疗',
                                    style: TextStyle(
                                        fontSize: 12,
                                        color: rgba(97, 142, 210, 1)))),
                          ),
                          spaceL(
                            AButton.normal(
                                width: 44,
                                height: 17,
                                bgColor: rgba(232, 243, 255, 1),
                                type: 'primary',
                                onPressed: () {},
                                child: Text('云计算',
                                    style: TextStyle(
                                        fontSize: 12,
                                        color: rgba(97, 142, 210, 1)))),
                          )
                        ],
                      ),
                      Row(children: [
                        SizedBox(
                          width: 12,
                          height: 12,
                          child: Image.asset(
                            'lib/assets/images/label/label.png',
                            fit: BoxFit.cover,
                          ),
                        ),
                        Text(
                          ' 项目融资、落地',
                          style: TextStyle(color: rgba(235, 101, 13, 1)),
                        )
                      ])
                    ],
                  ),
                )
              ],
            ),
          ),
          Container(
            color: rgba(250, 250, 250, 1),
            margin: EdgeInsets.only(top: 10, bottom: 10),
            padding: EdgeInsets.all(6),
            child: Column(
              children: [
                Text('2018年8月14日完成天使轮融资融资方为赛富投资基金 2019年4月15日完成A轮融资投资方有纳远明志…',
                    style:
                        TextStyle(color: rgba(107, 107, 107, 1), fontSize: 14)),
              ],
            ),
          ),
          Divider(color: rgba(235, 235, 235, 1))
        ]),
      ),
    );
  }

  Widget spaceL(Widget child) {
    return Container(
      margin: EdgeInsets.only(left: 10),
      child: child,
    );
  }
}

class ProjectList extends StatefulWidget {
  @override
  _ProjectListState createState() => _ProjectListState();
}

class _ProjectListState extends State<ProjectList> {
  List<ProjectData> projects = [];

  List tab = [
    {'title': '全部', 'key': 0},
    {'title': '项目', 'key': 1},
    {'title': '活动', 'key': 2},
    {'title': '需求', 'key': 3},
    {'title': '服务', 'key': 4}
  ];

  int active = 0;

  change(int index) {
    switch (index) {
      case 2:
        this.getActiveData();
        break;
      case 3:
        this.getNeedData();
        break;
      case 4:
        this.getServeData();
        break;
      default:
    }
    setState(() {
      active = index;
    });
  }

  @override
  void initState() {
    super.initState();
    this.getData();
  }

  void getData() async {
    MockData.projects.forEach((element) {
      ProjectData project = ProjectData.fromJson(element);
      projects.add(project);
    });
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      color: rgba(249, 249, 249, 1),
      // padding: EdgeInsets.symmetric(horizontal: 10),
      child: SingleChildScrollView(
        child: Container(
          color: rgba(249, 249, 249, 1),
          child: Column(
            children: [
              Container(
                color: rgba(255, 255, 255, 1),
                padding: EdgeInsets.only(top: 20, left: 10, bottom: 10),
                child: Row(
                    children: tab
                        .map((item) => TabTypeBtn(
                              title: item['title'],
                              active: active == item['key'],
                              press: () => change(item['key']),
                            ))
                        .toList()),
              ),
              active == 0
                  ? Column(
                      children: [GroupItem()],
                    )
                  : active == 1
                      ? Container(
                          padding: EdgeInsets.symmetric(horizontal: 10),
                          color: rgba(255, 255, 255, 1),
                          child: Column(
                            children: [Achievement(showFollow: true)],
                          ))
                      : active == 2
                          ? Column(
                              children: activeData
                                  .map((item) => ActivityListItem(
                                        showFollow: true,
                                        item: item,
                                      ))
                                  .toList(),
                            )
                          : active == 3
                              ? Container(
                                  padding: EdgeInsets.symmetric(horizontal: 10),
                                  color: rgba(255, 255, 255, 1),
                                  child: Column(
                                    children: needData
                                        .map((item) => NeedItem(
                                            showFollow: true, item: item))
                                        .toList(),
                                  ))
                              : Container(
                                  padding: EdgeInsets.symmetric(horizontal: 10),
                                  color: rgba(255, 255, 255, 1),
                                  child: Column(
                                    children: serveData
                                        .map((item) => ServeItem(
                                            showFollow: true, item: item))
                                        .toList(),
                                  )),
            ],
          ),
        ),
      ),
    );
  }

  List<ActiveData> activeData = [];
  void getActiveData() async {
    var res = await G.req.home.activityList();
    print(res);
    res.data['data']['list'].forEach((e) {
      ActiveData active = ActiveData.fromJson(e);
      activeData.add(active);
    });

    this.setState(() {});
  }

  List<NeedData> needData = [];
  void getNeedData() async {
    var res = await G.req.home.demandList();
    res.data['data']['list'].forEach((e) {
      NeedData list = NeedData.fromJson(e);
      needData.add(list);
    });

    this.setState(() {});
  }

  List<ServeData> serveData = [];
  void getServeData() async {
    var res = await G.req.home.serviceList();
    res.data['data']['list'].forEach((e) {
      ServeData list = ServeData.fromJson(e);
      serveData.add(list);
    });

    this.setState(() {});
  }
}
